import 'package:extended_text_field/extended_text_field.dart';
import 'package:flutter/material.dart';
import 'package:im_app/im/emojiOne/EmojiOneInfoes.dart';

import '../../emojiOne/EmojiOne.dart';

class ImSpecialTextSpanBuilder extends SpecialTextSpanBuilder {
  final TextStyle textStyle;

  ImSpecialTextSpanBuilder({ required this.textStyle});

  @override
  TextSpan build(String data, {TextStyle? textStyle, SpecialTextGestureTapCallback? onTap}) {
    var textSpan = super.build(data, textStyle: textStyle, onTap: onTap);
    return textSpan;
  }

  @override
  SpecialText? createSpecialText(String text, {
    TextStyle? textStyle,
    SpecialTextGestureTapCallback? onTap,
    required int index}) {
    if (text == null || text == "") return null;

    ///index is end index of start flag, so text start index should be index-(flag.length-1)
    if (isStart(text, EmojiText.START_FLAG)) {
      return EmojiText(textStyle!, onTap, start: index - (EmojiText.START_FLAG.length - 1),);
    }
    return null;
  }
}

class EmojiText extends SpecialText {
  static const String START_FLAG = "￼e{";
  static const String END_FLAG = "}";
  final int start;

  EmojiText(TextStyle textStyle, SpecialTextGestureTapCallback? onTap,
      { required this.start})
      : super(START_FLAG, END_FLAG, textStyle);

  @override
  InlineSpan finishText() {
    final String content = getContent();
    EmojiOne? emojiOne = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(content);
    var imagePath = emojiOne!.emojiPath??'';
    double fontSize = this.textStyle?.fontSize ?? 24;
    return ImageSpan(AssetImage(imagePath), imageWidth: fontSize, imageHeight: fontSize, actualText: toString());
  }
}